package com.ztz.mynewdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.ztz.mynewdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ContentActivity extends BaseActivity {
    public static final String CONTENT = "content";
    public static final String TITLES="titles";
    @BindView(R.id.web_view)
    WebView webView;
    String content;
    String titles;
    Button publishBtn;
    @BindView(R.id.content_progress)
    ProgressBar contentProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_content);
        ButterKnife.bind(this);
        content = getIntent().getStringExtra(CONTENT);
        titles=getIntent().getStringExtra(TITLES);
        publishBtn = (Button) findViewById(R.id.publish_dynamic);
        publishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ContentActivity.this,PublishDyActivity.class);
                intent.putExtra(PublishDyActivity.TITLE,titles);
                intent.putExtra(PublishDyActivity.CONTENTURL,content);
                startActivity(intent);
            }
        });
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.setWebViewClient(new WebViewClient());
        setToolbar("站酷");
        webView.loadUrl(content);
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //获取webview图片，长按保存
                WebView.HitTestResult result = ((WebView) v).getHitTestResult();
                if (null == result)
                    return false;
                int type = result.getType();
                if (type == WebView.HitTestResult.UNKNOWN_TYPE)
                    return false;
                switch (type) {
                    case WebView.HitTestResult.IMAGE_TYPE:
                        String saveImgUrl = result.getExtra();
                        Intent i = new Intent(ContentActivity.this, PhotoAcitivy.class);
                        i.putExtra(PhotoAcitivy.PHOTO, saveImgUrl);
                        startActivity(i);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
//        LAYER_TYPE_HARDWARE硬件加速
//        关闭硬件加速
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//        }
        //给webview设置进度条
        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100)
                {
                    contentProgress.setVisibility(View.GONE);
                }
                else {
                    contentProgress.setVisibility(View.VISIBLE);
                    contentProgress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        //判断webview是否滚动到底部
        //　getScrollY()方法返回的是当前可见区域的顶端距整个页面顶端的距离，也就是当前内容滚动的距离。
        //getHeight()或者getBottom()方法都返回当前webview这个容器的高度
        //getContentHeight返回的是整个html的高度，但并不等同于当前整个页面的高度，因为webview有缩放功能，所以当前整个页面的高度实际上应该是原始html的高度再乘上缩放比例。
//        if(webView.getContentHeight()*webView.getScale()==(webView.getHeight()+webView.getScrollY()))
//        {
//            Toast.makeText(this, "已经到底啦", Toast.LENGTH_SHORT).show();
//        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.share:
                UMImage thumb = new UMImage(this, R.mipmap.bxbxbai);
                UMImage image = new UMImage(ContentActivity.this, "imageuri");
                image.setThumb(thumb);
                new ShareAction(ContentActivity.this)
                        .withText("hello")
                        .withMedia(image)
                        .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                        .setCallback(new UMShareListener() {
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onResult(SHARE_MEDIA share_media) {
                                Toast.makeText(ContentActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                                Toast.makeText(ContentActivity.this, "分享失败" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {
                                Toast.makeText(ContentActivity.this, "分享取消", Toast.LENGTH_SHORT).show();
                            }
                        }).open();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }
}
